<form class="layui-form seller-form"  action="" >
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label seller-inline-2">教材名称：</label>
            <div class="layui-input-inline">
                <input type="text" name="book_name" lay-verify="title" placeholder="请输入名称关键字" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label seller-inline-2">ISBN：</label>
            <div class="layui-input-inline">
                <input type="text" name="book_isbn" lay-verify="title" placeholder="请输入ISBN" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label seller-inline-2">价格：</label>
            <div class="layui-input-inline" id="status_box">
                <select name="status" lay-filter="status" id="status" lay-search>
                    <option value=""></option>
                    <option value="0">未设置</option>
                    <option value="1">已设置</option>
                </select>
            </div>
        </div>
        <div class="layui-inline">
            <button class="layui-btn layui-btn-sm" lay-submit lay-filter="book-search"><i class="iconfont icon-chaxun"></i>筛选</button>
            <button id="add-bookinfo" class="layui-btn layui-btn-sm" lay-submit lay-filter="add-bookinfo"><i class="layui-icon">&#xe608;</i></i>添加</button> 
            <button type="button" class="layui-btn layui-btn-sm" lay-submit lay-filter="import-bookinfo"><i
                class="iconfont icon-cloudupload" style="font-size: 20px !important;" disabled></i>教材导入</button>
            <button type="button" class="layui-btn layui-btn-sm" lay-submit lay-filter="import-bookprice"><i
                class="iconfont icon-cloudupload" style="font-size: 20px !important;" disabled></i>教材定价导入</button>
            <button id="exportBtn" class="layui-btn layui-btn-sm layui-btn-disabled" lay-submit lay-filter="export-book"><i
                class="iconfont icon-clouddownload" style="font-size: 20px !important;" disabled></i>导出</button> 
        </div>
    </div>
</form>

<div class="table-body">
    <table id="bookTable" lay-filter="bookTable"></table>
</div>

<div id="exportBook" style="display: none;">
    <form class="layui-form export-form" action="">
        <div class="layui-form-item">
            <div class="layui-margin-10">
                <blockquote class="layui-elem-quote layui-text">
                    请先筛选要导出的教材，默认导出全部
                </blockquote>
            </div>

            <div class="layui-inline">
                <label class="layui-form-label">任务名称：</label>
                <input type="text" name="taskname" lay-verify="title" style="width:200px;" placeholder="请输入任务名称"
                    autocomplete="off" class="layui-input">
            </div>
        </div>
    </form>
</div>

<script>
    layui.use(['table','form','layer','laydate'],function(){
        var layer = layui.layer, table = layui.table,form = layui.form,date = layui.laydate;
        // 定义变量保存当前页面和当前页面数据条数
        var currPage; 
        var dataNum;

        //执行渲染
        table.render({
            elem: '#bookTable', //指定原始表格元素选择器（推荐id选择器）
            height: 'full-115',
            cellMinWidth: '80',
            page: 'true',
            limit:'20',
            id:'bookTable',
            url: "{:url('book/index')}",
            cols: [[ //标题栏
                {type:'numbers'},
                {field: 'book_name', title: '教材名称', width:200, align:'center'},
                {field: 'book_price', title: '教材单价', width:100, align:'center'},
                {field: 'book_disc', title: '折扣', width:100, align:'center'},
                {field: 'book_author', title: '主编', width:160, align:'center'},
                {field: 'book_concern', title: '出版社', width:180, align:'center'},
                {field: 'book_edition', title: '版次', width:160, align:'center'},
                {field: 'book_isbn', title: 'ISBN', width:160, align:'center'},
                {field: 'book_reward', title: '获奖情况', width:200, align:'center'},
                {field: 'book_date', title: '出版年限', width:120, align:'center'},
                { title: '操作', align: 'center', toolbar: '#bookInfoBar', width: 120 }
            ]], //设置表头
            done: function(res, curr, count){
                // 给变量赋值 
                dataNum = res.data.length;
                currPage = curr;

                // 判断加载结果数量是否大于0
                if(count>0){
                    $("#exportBtn").removeClass("layui-btn-disabled");
                    $('#exportBtn').removeAttr("disabled");
                } else {
                    $('#exportBtn').addClass("layui-btn-disabled");
                    $('#exportBtn').attr("disabled", true);
                }
            }
        });

        form.on('submit(book-search)', function(data){
            layui.table.reload('bookTable', {
                where: data.field
                ,page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });
        
        //教材增加
        layui.form.on('submit(add-bookinfo)', function (data) {
            var topLayui = parent === self ? parent.window.layui : top.parent.window.layui;
            topLayui.index.openTabsPage('{:url("Book/addBookInfo")}', '教材增加'); 
            return false;
        }); 
        
        //教材导入
        layui.form.on('submit(import-bookinfo)', function (data) {
            var topLayui = parent === self ? parent.window.layui : top.parent.window.layui;
            topLayui.index.openTabsPage('{:url("Book/importBookInfo")}', '导入教材信息'); 
            return false;
        }); 
        
        //导入教材定价信息
        layui.form.on('submit(import-bookprice)', function (data) {
            var topLayui = parent === self ? parent.window.layui : top.parent.window.layui;
            topLayui.index.openTabsPage('{:url("Book/importBookPrice")}', '导入教材定价信息'); 
            return false;
        }); 

        //学生信息导出
        layui.form.on('submit(export-book)', function (data) {
            layer.open({
                type: 1,
                title: '教材信息导出',
                area: ['400px', '248px'], //宽高
                btn: ['确定', '取消'],
                content: $("#exportBook").html(),
                success: function () {
                    $(':focus').blur();
                    this.enterEsc = function(event){
                        if(event.keyCode === 13){
                            if(!$(".layui-layer-btn0").hasClass("layui-btn-disabled")){
                                $(".layui-layer-btn0").addClass("layui-btn-disabled");

                                //判断是否有选中
                                var filter = $(".seller-form").serialize();
                                $(".export-form:last").append("<input type='hidden' name='filter' value='" + filter + "'>");
                                var data = $(".export-form:last").serializeArray();
                                data.push({ 'name': 'model', 'value': 'BookInfo' });

                                if($.trim(data[0]['value']) == '') {
                                    layer.msg('请填写任务名称！', { time: 1300 });
                                    $(".layui-layer-btn0").removeClass("layui-btn-disabled");
                                    return false;
                                }

                                JsPost("{:url('Ietask/export')}", data, function (res) {
                                        layer.msg(res.msg, { time: 1500 }, function () {
                                            if (res.status) {
                                                layer.closeAll();
                                                //执行跳转
                                                var topLayui = parent === self ? layui : top.layui;
                                                topLayui.index.openTabsPage('/manage/ietask/index.html', '导入导出');
                                            } else {
                                                $(".layui-layer-btn0").removeClass("layui-btn-disabled");
                                            }
                                        });
                                    }
                                );
                            }
                            return false; //阻止系统默认回车事件
                        }
                    };
                    $(document).on('keydown', this.enterEsc);	//监听键盘事件，关闭层
                },
                yes: function () {
                    if(!$(".layui-layer-btn0").hasClass("layui-btn-disabled")){
                        $(".layui-layer-btn0").addClass("layui-btn-disabled");

                        //判断是否有选中
                        var filter = $(".seller-form").serialize();
                        $(".export-form:last").append("<input type='hidden' name='filter' value='" + filter + "'>");
                        var data = $(".export-form:last").serializeArray();
                        if($.trim(data[0]['value']) == '') {
                            layer.msg('请填写任务名称！', { time: 1300 });
                            $(".layui-layer-btn0").removeClass("layui-btn-disabled");
                            return false;
                        }

                        data.push({ 'name': 'model', 'value': 'BookInfo' });
                        JsPost("{:url('Ietask/export')}", data, function (res) {
                                layer.msg(res.msg, { time: 1500 }, function () {
                                    if (res.status) {
                                        layer.closeAll();
                                        //执行跳转
                                        var topLayui = parent === self ? layui : top.layui;
                                        topLayui.index.openTabsPage('/manage/ietask/index.html', '导入导出');
                                    } else {
                                        $(".layui-layer-btn0").removeClass("layui-btn-disabled");
                                    }
                                });
                            }
                        );
                    }
                }, btn2: function () {
                    layer.closeAll();
                }, end: function(){
                    $(document).off('keydown', this.enterEsc);	        //解除键盘关闭事件
                }
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });

        //监听工具条
        table.on('tool(bookTable)', function (obj) { //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            if (layEvent === 'del') { //删除
                layer.confirm('真的要删除么', { icon: 3 }, function (index) {
                    JsGet("{:url('Book/delBookInfo')}?id=" + data.id, function (res) {
                        //删除逻辑中判断只有一条数据时应该刷新前一页信息
                        layer.msg(res.msg);
                        if(res.status){
                            if(dataNum == 1){
                                currPage = currPage - 1;
                            }

                            table.reload('bookTable', {
                                page: {
                                    curr: currPage
                                }
                            });
                            return false;
                        }
                    })
                });
            } else if (layEvent === 'edit') { //编辑 
                var editUrl = "{:url('Book/editBookInfo')}?id=" + data.id;
                var topLayui = parent === self ? parent.window.layui : top.parent.window.layui;
                topLayui.index.openTabsPage(editUrl, '编辑教材');
                return false;
            }
        });
    })
</script>

<script type="text/html" id="bookInfoBar">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>